<?php

	include 'app.render.php';
	
	$function = $_GET['function'];
	
	switch($function){
		case "out":
			$id = $_GET['id'];
			
			$results = $database->select("SELECT * FROM ads WHERE id = '$id' " );
			
			$add_count = $results[0]['clicks'];
    		$vars['clicks'] = $add_count  + 1;
    		
    		$database->update_array('ads', $vars, "id = ".$results[0]['id']."");
    	
    		header("Location: ".$results[0]['url']."");
    		break;
    	
    	case "serve":
    		$id = $_GET['id'];
    		
    		$results = $database->select("SELECT * FROM ads WHERE name = '$id' ORDER BY RAND() " );
    	
    		if($results[0]['target'] != ''){
            $target = 'target=_blank';
    		} else {
            $target = '';
    		}
    
    		header("content-type: application/x-javascript");    
    		
    		echo '
    		document.write("<a href=ads.php?function=out&id='.$results[0]['id'].' '.$target.'>
    			<img src=\'UserFiles/Image/product_photos/'. 
    		
    		$results[0]['image'].'\' border=0></a>");';
    		$add_count = $results[0]['imp'];
    		$vars['imp'] = $add_count  + 1;
    		$database->update_array('ads', $vars, "id = ".$results[0]['id']."");
    		break;
	}
?>
